//
//  DateExtension.swift
//  FoundationsAppCode
//
//  Created by Erickson on 16/3/18.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import Foundation

extension String {
    func date()->String{
        
        return self.date("yyyy-MM-dd - HH:mm:ss")
    }
    
    func date(_ dateFormat:String)->String {
        let outputFormat = DateFormatter()
        let myDataSource: NSString = self as NSString
        //yyyy-MM-dd - HH:mm:ss
        outputFormat.dateFormat = dateFormat
        
        let pubTime = Date(timeIntervalSince1970: myDataSource.doubleValue)
        
        return outputFormat.string(from: pubTime)
    }

    
}


extension Int {
    /*
     int days=((int)time)/(3600*24);
     int hours=((int)time)%(3600*24)/3600;
     NSString *dateContent=[[NSString alloc] initWithFormat:@"%i天%i小时",days,hours];

     
     */
    
    /*
     
     
     //计算天数
     $timediff = $endtime-$starttime;
     $days = intval($timediff/86400);
     //计算小时数
     $remain = $timediff%86400;
     $hours = intval($remain/3600);
     //计算分钟数
     $remain = $remain%3600;
     $mins = intval($remain/60);
     //计算秒数
     $secs = $remain%60;
     $res = array("day" => $days,"hour" => $hours,"min" => $mins,"sec" => $secs);
     return $res;
     */
    func archiveDate()->String {
        print("=======\(self)")
        var perTimes = ""
        
        let eachDay = (3600 * 24)
        let days = self / eachDay
        let hours = self % eachDay / 3600
        
        let min = ((self % eachDay) % 3600) / 60
        
        
        if self < 0 {
            return "0分"
        }
        if days > 0 {
            perTimes = "\(days)天\(hours)小时\(min)分"
            return perTimes;
        }
        
        if hours > 0 {
            perTimes = "\(hours)小时\(min)分"
            return perTimes
        }
        
        return "\(min)分"
        
    }
}

extension Date {
    func isThisYear()->Bool {
        let calendar = Calendar.current
        let dateCmps = (calendar as NSCalendar).components(.year, from: self)
        let nowCmps = (calendar as NSCalendar).components(.year, from: Date())
        return dateCmps.year == nowCmps.year
    }
    
    func isYesterday() ->Bool {
        var now = Date()
        let fmt = DateFormatter()
        fmt.dateFormat = "yyyy-MM-dd"
        let dateStr = fmt.string(from: self)
        let nowStr = fmt.string(from: now)
        let date = fmt.date(from: dateStr)
        now = fmt.date(from: nowStr)!
        let calendar = Calendar.current
        let unit : NSCalendar.Unit = [.year,.month,.day]
        let cmps = (calendar as NSCalendar).components(unit, from: date!, to: now, options: .wrapComponents)
        return cmps.year == 0 && cmps.month == 0 && cmps.day == 1
    }
    
    
    func isToday() -> Bool {
        let now = Date()
        
        let fmt = DateFormatter()
        fmt.dateFormat = "yyyy-MM-dd"
        let dateStr = fmt.string(from: self)
        let nowStr = fmt.string(from: now)
        return dateStr == nowStr
        
    }
}
